//
//  AppDelegate+WeChat.m
//  BaseProject
//
//  Created by 王盼盼 on 2024/9/11.
//  Copyright © 2024 wangpanpan. All rights reserved.
//

#import "AppDelegate+WeChat.h"
@implementation AppDelegate (WeChat)
- (void)initWeChat {
    //在register之前打开log, 后续可以根据log排查问题
    //[WXApi startLogByLevel:WXLogLevelDetail logBlock:^(NSString * _Nonnull log) {
        //NSLog(@"WeChatSDK: %@", log);
    //}];
    //务必在调用自检函数前注册
    [WXApi registerApp:S.appKey_weixin universalLink:S.universalLink];
    //调用自检函数
    //[WXApi checkUniversalLinkReady:^(WXULCheckStep step, WXCheckULStepResult * _Nonnull result) {
        //NSLog(@"checkUniversalLinkReady %@, %u, %@, %@", @(step), result.success, result.errorInfo, result.suggestion);
    //}];
}
- (BOOL)isWeChatScheme:(NSURL *)url {
    return [S.appKey_weixin isEqualToString:url.scheme];
}
- (BOOL)application_wechat:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
    // url.host 分享：platformId=wechat 自检：resendContextReqByScheme 登录：oauth
    return [WXApi handleOpenURL:url delegate:self];
}
- (BOOL)application_wechat:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler {
    return [WXApi handleOpenUniversalLink:userActivity delegate:self];
}
#pragma mark - WXApiDelegate
- (void)onReq:(BaseReq *)req {
    
}
- (void)onResp:(BaseResp *)resp {
    if ([resp isKindOfClass:[PayResp class]]) {
        [self handlePayResp:(PayResp *)resp];
    } else if ([resp isKindOfClass:[SendMessageToWXResp class]]) {
        [self handleSendMessageToWXResp:(SendMessageToWXResp *)resp];
    } else if ([resp isKindOfClass:[SendAuthResp class]]) {
        [self handleSendAuthResp:(SendAuthResp *)resp];
    }
}
- (void)handlePayResp:(PayResp *)req {
    //支付返回的结果, 实际支付结果需要去微信服务器端查询
    NSString *isPaySuccess = @"0";
    NSString *msg = @"";
    NSNumber *errorCode = @(req.errCode);
    switch (req.errCode) {
        case WXSuccess:
            isPaySuccess = @"1";
            msg = @"支付成功";
            break;
        case WXErrCodeUserCancel:
            isPaySuccess = @"0";
            msg = @"用户取消了支付";
            break;
        default:
            isPaySuccess = @"0";
            msg = @"支付失败";
            break;
    }
    NSMutableDictionary *payDic = @{}.mutableCopy;
    [payDic setValue:msg forKey:@"memo"];
    [payDic setValue:msg forKey:@"msg"];
    [payDic setValue:@"wechat" forKey:@"payType"];
    [payDic setValue:errorCode forKey:@"code"];
    [payDic setValue:isPaySuccess forKey:@"resultStatus"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"payResultNotification" object:nil userInfo:payDic];
}
- (void)handleSendMessageToWXResp:(SendMessageToWXResp *)req {
    switch (req.errCode) {
        case WXSuccess:
            [Utilities showToast:@"  分享成功  "];
            break;
        default:
            break;
    }
}
- (void)handleSendAuthResp:(SendAuthResp *)req {
    if (![req.state isEqualToString:S.state_weixin]) {
        [Utilities showToast:@"微信授权失败"];
        return;
    }
    if (req.code.length <= 0) {
        [Utilities showToast:@"微信授权失败"];
        return;
    }
    [Utilities setToNSUserDefaults:@"BA_marryOauth" value:req.code];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter]postNotificationName:@"BA_marryOauth" object:nil];
    });
}
@end
